<?php

namespace App\Admin\Forms;

use App\Admin\Renderable\GoodsRenderable;
use App\Http\Logic\PayLogic;
use App\Logic\GoodsLogic;
use App\Models\SwtGood;
use App\Models\SwtMachineGood;
use App\Models\SwtOrder;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class PayOrderForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;

        $order_info = SwtOrder::with(['machine']) -> find($id);

        $goods_info = GoodsLogic::getOrderGoodsList($id, $order_info -> machine -> source_type, $input['goods_list']);

        $res = PayLogic::goPay($order_info, $goods_info['amount'], $goods_info['goods_list']);

        if($res === false) {
            return $this->response()->error(PayLogic::getErrorMessage());
        }

        $order_info -> is_warning = 0;
        $order_info -> warning_desc = '';
        $order_info -> save();

        return $this->response()->success('扣款成功')->refresh();
    }

    public function form()
    {
        $id = $this->payload['id'] ?? null;

        $order_info = SwtOrder::with(['machine']) -> where('id',$id)->firstOrFail();

        $this -> disableResetButton();

        $this -> table('goods_list','', function(Form $form) use ( $order_info ){
            switch($order_info -> machine -> source_type)
            {
                case 1:
                    $form -> selectTable('goods_id','商品')
                        -> attribute('style', 'width:200px;')
                        -> title('商品表')
                        -> from(GoodsRenderable::make())
                        -> rules('required', [ 'required' => '商品名不能为空' ])
                        -> options(function ($v){
                            if (!$v) {
                                return [];
                            }
                            return SwtGood::find($v)->pluck('goods_name_price', 'id');
                        })
                        -> pluck('goods_name_price')
                        -> required();
                break;
                case 2:
                    $form -> selectTable('goods_id','商品')
                        -> attribute('style', 'width:200px;')
                        -> title('商品表')
                        -> from(GoodsRenderable::make(['machine_id' => $order_info -> machine -> id]))
                        -> rules('required', [ 'required' => '商品名不能为空' ])
                        -> options(function ($v){
                            if (!$v) {
                                return [];
                            }
                            return SwtMachineGood::where('goods_id', $v)->pluck('goods_name_price', 'id');
                        })
                        -> pluck('goods_name_price')
                        -> required();
                break;
            }


            $form -> text('goods_num','数量')
                -> type('number')
                -> attribute('style', 'width:80px;')
                -> rules('required', [ 'required' => '数量不能为空'])
                -> attribute('min', 0)
                -> attribute('max', 100)
                -> default(1)
                -> required();

        }) -> saving(function ($data) {
            if(count($data) <= 0) {
                return $this->response()->error('订单商品不能为空');
            }

            $list = [];

            foreach($data as $v) {
                if(key_exists($v['goods_id'],$list)) {
                    $list[$v['goods_id']]['goods_num'] += $v['goods_num'];
                } else {
                    $list[$v['goods_id']] = $v;
                }
            }

            return array_merge($list);
        });
    }
    public function default()
    {
        $id = $this->payload['id'];

        $order_info = SwtOrder::with(['goods']) -> where('id',$id)->firstOrFail();

        return [
            'goods_list'  => $order_info -> goods
        ];
    }
}
